package org.ayatsuji.savetempdata;

import android.os.Bundle;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
 * @author ayatsuji
 * @date 2021-09-25 00:35
 * @since
 */
public class MainActivity extends AppCompatActivity {
    private final String tempDataKey = "tempData";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);

        if (savedInstanceState != null) {
            // 这里取出之前当前活动销毁时在onSaveInstanceState存放的数据
            String tempData = savedInstanceState.getString(tempDataKey);
            Log.d(getClass().getName(), tempDataKey + " is [" + tempData + "]");
        }
    }

    // 销毁前可以在这里存放临时数据，下次创建当前活动再从onCreate取出
    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        String tempData = "i am the temp data";
        outState.putString(tempDataKey, tempData);
        Log.d(getClass().getName(), "save instance state...");
    }
}
